package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartApiController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车控制器
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request) {
        //【登录】  获取用户id 从封装的AuthContextHolder中获取  userId在请求体中
        String userId = AuthContextHolder.getUserId(request);
        //【未登录】  userId = userTempId
        if (StringUtils.isEmpty(userId)) {//如果未登录没有用户id 就将userId设置为临时用户id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //调用方法
        cartService.addToCart(skuId, userId, skuNum);
        //返回结果
        return Result.ok();
    }

    //查看购物车  /api/cart/cartList
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request) {
        //获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //调用方法
        List<CartInfo> cartInfoList = this.cartService.cartList(userId, userTempId);
        //返回数据
        return Result.ok(cartInfoList);
    }

    //  修改购物项的状态
    //  /api/cart/checkCart/{skuId}/{isChecked}
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request) {

        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法
        this.cartService.checkCart(skuId, userId, isChecked);

        //  返回数据
        return Result.ok();
    }

    //  删除购物车
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request) {

        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法
        this.cartService.deleteCart(skuId, userId);

        //  返回数据
        return Result.ok();
    }

    //   通过id查询购物车列表/api/cart/getCartCheckedList/{userId}
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId) {
        return cartService.getCartCheckedList(userId);
    }
}
